/**
 * @name zhangyy-g@grandsoft.com.cn
 * @description zhangyy's code
 * Date: 15-4-17
 * Time: 下午5:46
 *
 */
$(function () {
    FastClick.attach(document.body);
    /**各种事件监听**/
    var container = $('#tongbupan')
    var pageConfig = window.pageConfig

    //监听各个事件
    //初始化各个面板
    $.jps.subscribe('dock-init', function (panel) {
        App.modules.docList.init(panel.listPanel, pageConfig)
    })

    $.jps.subscribe('file-list', function (options) {
        var filter = options.filter
        var path = options.path
        var filterInfo = App.common.modules.util.filterInfo(filter)
        var ajax = App.common.modules.util.getUrl({
            action: path ? 'list' : (options.action || filterInfo.action),
            path: path || filterInfo.path
        })
        App.modules.docList.load(ajax.url, filter, ajax.data, function (collection, response, options) {
            var data = {
                path: filterInfo.path
            }
            if(response.code == 0){
                data = response.data.data || response.data
            }
            data.filter = filter
            App.modules.layout.updateHeader(data)
            App.modules.layout.updateOpt(data)
        })
    })

    //搜索
    $.jps.subscribe('search-update', function () {
        App.modules.layout.clearSearch()
    })

    var searchAjax = null
    $.jps.subscribe('file-search', function (options) {
        var key = options.key
        var path = options.path

        searchAjax && searchAjax.abort()

        searchAjax = $.ajax({
            url: '/review/file/space/filetext/',
            method: 'post',
            dataType: 'json',
            global: false,
            data: {
                id: '',
                query: key,
                range: '',
                filepath: path,
                filename: '',
                filetype: '',
                author: '',
                content: '',
                filetag: '',
                sizeif: 'gt',
                sizenum: '',
                sizetype: 'mb',
                timestart: '',
                timeend: '',
                sort: 'asc',
                sortfield: 'filename',
                filepage: 1,
                filesize: 500,
                fileCont: '',
                characterNum: '40,20'
            },
            success: function (data) {
                if (data.code == 0) {
                    var id = data.data.id
                    $.ajax({
                        url: '/review/file/search/filetext/',
                        method: 'post',
                        dataType: 'json',
                        global: false,
                        data: {
                            p: 0,
                            s: 10,
                            path: 'filter:搜索',
                            orderby: 'lastmodifiedtime',
                            ordertype: 'desc',
                            id: id
                        },
                        success: function (data) {
                            App.modules.layout.hideFilterPanel()
                            App.modules.docList.resetCollection(data.data.subfiles, 'search')
                            App.modules.layout.updateHeader({
                                filter: 'search'
                            })

                            App.modules.layout.updateOpt({
                                filter: 'search'
                            })
                        }
                    })
                }
            }
        })
    })

    $.jps.subscribe('filter-show', function () {
        App.modules.layout.showFilterPanel()
        App.modules.layout.updateHeader({
            filter: ''
        })

        App.modules.layout.updateOpt({
            filter: ''
        })
    })

    //预览图片
    $.jps.subscribe('img-preview', function (file) {
        App.modules.preview.img(file)
    })

    //预览office
    $.jps.subscribe('office-preview', function (file) {
        App.modules.preview.office(file)
    })

    //点击opt
    $.jps.subscribe('action-click', function (options) {
        var items = App.modules.docList.getCheckedItems()
        options.fileItems = items
        App.modules.action.init(container, options)
    })

    $.jps.subscribe('action-back', function(needFileList) {
        App.modules.action.hide(needFileList)
    })

    if(browser.versions.mobile){
        document.addEventListener('deviceready', function(){
            App.modules.layout.init(container, pageConfig)
        }, false);
    }else{
        App.modules.layout.init(container, pageConfig)
    }

    /**全局配置**/
    $(document).ajaxError(function (e, xhr, ajaxSettings, thrownError) {
        /*if (xhr.status === 404) {
         //window.location.href = App.modules.util.rootPath + '/error/404'
         return
         }*/
        App.common.modules.smallnote(xhr.responseText || '系统异常，请重试', {
            pattern: 'error'
        })
    })

    $(document).ajaxSuccess(function (e, xhr, ajaxSettings) {
        var data = JSON.parse(xhr.responseText)
        if (data.code != 0) {
            App.common.modules.smallnote(data.message, {
                pattern: 'error'
            })
        }
    })
})
